/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unipampa.view;

import br.unipampa.dados.DBCompromisso;
import br.unipampa.dados.DBConection;
import br.unipampa.model.Calendario;
import br.unipampa.utilitarios.ExecutaSom;
import br.unipampa.utilitarios.TrataDataHora;
import br.unipampa.utilitarios.Utilitaria;
import java.awt.Color;
import java.awt.Component;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Gabriel Moro
 */
public class PaneCompromisso extends javax.swing.JPanel implements EfeitosSonoros {

    //MODELOS DE TABELAS
    private DefaultTableModel modeloCalendar;
    private DefaultTableModel modeloTableAgenda;
    private DefaultTableCellRenderer modeloCelTableCal;
    private TableColumn columnModel;
    private TableColumn columnModelII;
    //ATRIBUTOS PARA ORGANIZAÇÃO
    private int diaSelecionado;
    private String mesSelecionado;
    private String anoSelecionado;
    //MODEL
    private TrataDataHora trataData;
    private Calendario calendar;
    //BANCO
    private DBConection conexao;
    private DBCompromisso dbCompromisso;
    //OPERACOES
    private CadastroDeCompromisso cadastroComp;
    private EdicaoDeCompromisso edicaoComp;

    /**
     * Creates new form PaneCompromisso
     */
    public PaneCompromisso() {
        initComponents();

        this.modeloTableAgenda = (DefaultTableModel) this.jTableCompromissos.getModel();
        this.modeloCalendar = (DefaultTableModel) this.jTableCalendario.getModel();
        this.modeloCalendar = (DefaultTableModel) this.jTableCalendario.getModel();
        this.modeloCelTableCal = new DefaultTableCellRenderer();
        this.modeloCelTableCal.setBackground(Color.LIGHT_GRAY);
        this.modeloCelTableCal.setForeground(Color.RED);
        //Definindo cellrenderer para a primeira coluna
        this.jTableCalendario.getColumn("Dm").setCellRenderer(modeloCelTableCal);
        this.jTableCompromissos.getTableHeader().setReorderingAllowed(false);
        this.jPanelHoraCompr.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelCalendar = new javax.swing.JPanel();
        jScrollPaneCalendar = new javax.swing.JScrollPane();
        jTableCalendario = new javax.swing.JTable();
        jComboMesesCalendar = new javax.swing.JComboBox();
        jComboAnoCalendar = new javax.swing.JComboBox();
        btnGo = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPaneTxtAreaCalend = new javax.swing.JScrollPane();
        jTextAreaDatas = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jPanelHoraCompr = new javax.swing.JPanel();
        jScrollTabelaAgenda = new javax.swing.JScrollPane();
        jTableCompromissos = new javax.swing.JTable();
        lblDate = new javax.swing.JLabel();
        jPanelAcoes = new javax.swing.JPanel();
        btnNovoComp = new javax.swing.JButton();
        btnEditarComp = new javax.swing.JButton();
        btnExcluirComp = new javax.swing.JButton();
        btnAtualizatabelaHoraDAta = new javax.swing.JButton();

        panelCalendar.setBackground(new java.awt.Color(204, 204, 255));
        panelCalendar.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calendário, clique no dia e cadastre,veja e edite os compromissos!", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 14), new java.awt.Color(51, 51, 51))); // NOI18N

        jTableCalendario.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jTableCalendario.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Dm", "Sg", "Tr", "Qr", "Qn", "Se", "Sb"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCalendario.setColumnSelectionAllowed(true);
        jTableCalendario.getTableHeader().setResizingAllowed(false);
        jTableCalendario.getTableHeader().setReorderingAllowed(false);
        jTableCalendario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableCalendarioMouseClicked(evt);
            }
        });
        jScrollPaneCalendar.setViewportView(jTableCalendario);

        jComboMesesCalendar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--------------", "Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro" }));
        jComboMesesCalendar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboMesesCalendarActionPerformed(evt);
            }
        });

        jComboAnoCalendar.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "------", "2013", "2014", "2015" }));

        btnGo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/pesquisa.png"))); // NOI18N
        btnGo.setToolTipText("Pesquisar no calendário");
        btnGo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGoActionPerformed(evt);
            }
        });
        btnGo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnGoKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Ano:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Mês:");

        jTextAreaDatas.setEditable(false);
        jTextAreaDatas.setBackground(new java.awt.Color(204, 255, 255));
        jTextAreaDatas.setColumns(20);
        jTextAreaDatas.setRows(5);
        jScrollPaneTxtAreaCalend.setViewportView(jTextAreaDatas);

        jLabel3.setText("Você tem compromisso(s) nos dias:");

        javax.swing.GroupLayout panelCalendarLayout = new javax.swing.GroupLayout(panelCalendar);
        panelCalendar.setLayout(panelCalendarLayout);
        panelCalendarLayout.setHorizontalGroup(
            panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCalendarLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelCalendarLayout.createSequentialGroup()
                        .addComponent(jScrollPaneCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPaneTxtAreaCalend)))
                    .addGroup(panelCalendarLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboMesesCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboAnoCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnGo, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        panelCalendarLayout.setVerticalGroup(
            panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCalendarLayout.createSequentialGroup()
                .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelCalendarLayout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(btnGo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCalendarLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboAnoCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jComboMesesCalendar)
                            .addComponent(jLabel2))
                        .addGap(15, 15, 15)))
                .addGroup(panelCalendarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPaneCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(panelCalendarLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPaneTxtAreaCalend, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        jPanelHoraCompr.setBackground(new java.awt.Color(204, 204, 255));

        jTableCompromissos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Hora", "Compromisso"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableCompromissos.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jTableCompromissos.getTableHeader().setResizingAllowed(false);
        jTableCompromissos.getTableHeader().setReorderingAllowed(false);
        jTableCompromissos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTableCompromissosKeyPressed(evt);
            }
        });
        jScrollTabelaAgenda.setViewportView(jTableCompromissos);

        lblDate.setBackground(new java.awt.Color(0, 0, 0));
        lblDate.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        lblDate.setText("dd/mm/aaaa");

        jPanelAcoes.setBackground(new java.awt.Color(204, 204, 204));
        jPanelAcoes.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Ações", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP, null, new java.awt.Color(0, 0, 0)));
        jPanelAcoes.setForeground(new java.awt.Color(204, 204, 204));
        jPanelAcoes.setPreferredSize(new java.awt.Dimension(88, 177));

        btnNovoComp.setBackground(new java.awt.Color(153, 180, 209));
        btnNovoComp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/novo.png"))); // NOI18N
        btnNovoComp.setToolTipText("Cadastrar novo compromisso");
        btnNovoComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoCompActionPerformed(evt);
            }
        });

        btnEditarComp.setBackground(new java.awt.Color(153, 180, 209));
        btnEditarComp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/editar.png"))); // NOI18N
        btnEditarComp.setToolTipText("Editar compromisso");
        btnEditarComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarCompActionPerformed(evt);
            }
        });

        btnExcluirComp.setBackground(new java.awt.Color(153, 180, 209));
        btnExcluirComp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/lixeira.png"))); // NOI18N
        btnExcluirComp.setToolTipText("Excluir compromisso");
        btnExcluirComp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExcluirCompActionPerformed(evt);
            }
        });

        btnAtualizatabelaHoraDAta.setBackground(new java.awt.Color(153, 180, 209));
        btnAtualizatabelaHoraDAta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unipampa/icons/atualiza.png"))); // NOI18N
        btnAtualizatabelaHoraDAta.setToolTipText("Atualiza Compromissos");
        btnAtualizatabelaHoraDAta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtualizatabelaHoraDAtaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelAcoesLayout = new javax.swing.GroupLayout(jPanelAcoes);
        jPanelAcoes.setLayout(jPanelAcoesLayout);
        jPanelAcoesLayout.setHorizontalGroup(
            jPanelAcoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAcoesLayout.createSequentialGroup()
                .addGroup(jPanelAcoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovoComp, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditarComp, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnExcluirComp, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAtualizatabelaHoraDAta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelAcoesLayout.setVerticalGroup(
            jPanelAcoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAcoesLayout.createSequentialGroup()
                .addComponent(btnAtualizatabelaHoraDAta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnNovoComp, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEditarComp)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnExcluirComp)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanelHoraComprLayout = new javax.swing.GroupLayout(jPanelHoraCompr);
        jPanelHoraCompr.setLayout(jPanelHoraComprLayout);
        jPanelHoraComprLayout.setHorizontalGroup(
            jPanelHoraComprLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelHoraComprLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelAcoes, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanelHoraComprLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelHoraComprLayout.createSequentialGroup()
                        .addComponent(lblDate)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollTabelaAgenda))
                .addContainerGap())
        );
        jPanelHoraComprLayout.setVerticalGroup(
            jPanelHoraComprLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelHoraComprLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelHoraComprLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelHoraComprLayout.createSequentialGroup()
                        .addComponent(lblDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollTabelaAgenda, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelHoraComprLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanelAcoes, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(panelCalendar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelHoraCompr, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelCalendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelHoraCompr, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTableCalendarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableCalendarioMouseClicked
        int row = this.jTableCalendario.getSelectedRow();
        int column = this.jTableCalendario.getSelectedColumn();

        Object diaObj = jTableCalendario.getValueAt(row, column);

        if (diaObj != null) {
            this.diaSelecionado = Utilitaria.converteParaInteiro(diaObj);
            String date = this.diaSelecionado + "/" + this.mesSelecionado + "/" + this.anoSelecionado;
            this.lblDate.setText(date);
            preencheTabelaAgenda(this.diaSelecionado, this.mesSelecionado, this.anoSelecionado);
            jScrollTabelaAgenda.revalidate();
            this.jPanelHoraCompr.setVisible(true);
            this.jPanelHoraCompr.revalidate();
        }

    }//GEN-LAST:event_jTableCalendarioMouseClicked

    private void jComboMesesCalendarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboMesesCalendarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboMesesCalendarActionPerformed

    private void btnGoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGoActionPerformed
        efeitoSonoro();
        go();
    }//GEN-LAST:event_btnGoActionPerformed

    private void btnNovoCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoCompActionPerformed
        efeitoSonoro();
        String dia = Utilitaria.converteParaTexto(this.diaSelecionado);
        this.cadastroComp = new CadastroDeCompromisso(dia, this.mesSelecionado, this.anoSelecionado);
        this.cadastroComp.setVisible(true);
    }//GEN-LAST:event_btnNovoCompActionPerformed

    private void btnEditarCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarCompActionPerformed
        efeitoSonoro();
        editarComprEvent();
    }//GEN-LAST:event_btnEditarCompActionPerformed

    private void btnExcluirCompActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExcluirCompActionPerformed
        efeitoSonoro();
        deletarComprEvent();
    }//GEN-LAST:event_btnExcluirCompActionPerformed

    private void btnAtualizatabelaHoraDAtaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtualizatabelaHoraDAtaActionPerformed
        efeitoSonoro();
        atualizaComprEvent();
    }//GEN-LAST:event_btnAtualizatabelaHoraDAtaActionPerformed

    private void btnGoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnGoKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            efeitoSonoro();
            go();
        }
    }//GEN-LAST:event_btnGoKeyPressed

    private void jTableCompromissosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableCompromissosKeyPressed
        if (evt.getKeyCode() == evt.VK_E) {
            efeitoSonoro();
            editarComprEvent();
        }
        if (evt.getKeyCode() == evt.VK_DELETE) {
            efeitoSonoro();
            deletarComprEvent();
        }
        if (evt.getKeyCode() == evt.VK_F5) {
            efeitoSonoro();
            atualizaComprEvent();
        }
    }//GEN-LAST:event_jTableCompromissosKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAtualizatabelaHoraDAta;
    private javax.swing.JButton btnEditarComp;
    private javax.swing.JButton btnExcluirComp;
    private javax.swing.JButton btnGo;
    private javax.swing.JButton btnNovoComp;
    private javax.swing.JComboBox jComboAnoCalendar;
    private javax.swing.JComboBox jComboMesesCalendar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelAcoes;
    private javax.swing.JPanel jPanelHoraCompr;
    private javax.swing.JScrollPane jScrollPaneCalendar;
    private javax.swing.JScrollPane jScrollPaneTxtAreaCalend;
    private javax.swing.JScrollPane jScrollTabelaAgenda;
    private javax.swing.JTable jTableCalendario;
    private javax.swing.JTable jTableCompromissos;
    private javax.swing.JTextArea jTextAreaDatas;
    private javax.swing.JLabel lblDate;
    private javax.swing.JPanel panelCalendar;
    // End of variables declaration//GEN-END:variables

    public void preencheTabelaAgenda(int dia, String mes, String ano) {
        this.modeloTableAgenda.setNumRows(0);//limpa toda a tabela
        try {
            this.conexao = new DBConection();
            this.dbCompromisso = new DBCompromisso(this.conexao.getCon());
            ArrayList<Object> valores = this.dbCompromisso.selected("definicao_compromisso,codAno_compromisso,codMes_compromisso,codDia_compromisso,codHora_compromisso", dia, mes, ano);
            if (valores.size() != 0) {
                for (int x = 0; x < valores.size(); x = x + 2) {
                    this.modeloTableAgenda.addRow(new Object[]{
                        valores.get(x),
                        valores.get(x + 1)
                    });
                }
            } else {
                ApoioDeInterface.mensagemNaoExisteCompromisso();
            }
        } catch (SQLException erroSql) {
            ApoioDeInterface.mensagemErroBanco(erroSql.getMessage());
        } finally {
            this.conexao.encerrarConexao();
            this.dbCompromisso.closedConnection();
            redimensionarColumnsComp();
        }
    }

    private boolean validaCombosCalendar() {
        if (this.jComboAnoCalendar.getSelectedIndex() == 0
                || this.jComboMesesCalendar.getSelectedIndex() == 0
                || (this.jComboAnoCalendar.getSelectedIndex() == 0
                && this.jComboMesesCalendar.getSelectedIndex() == 0)) {
            return false;
        } else {
            return true;
        }
    }

    private void validaCompromissosCalendar(ArrayList<Integer> result) {
        this.jTextAreaDatas.setText("");
        if (result == null || result.isEmpty()) {
            this.jTextAreaDatas.append("Mês sem compromissos...");
        } else {
            int col = 0;
            int totalDeLinhas = this.jTableCalendario.getRowCount();
            int contadorAuxiliar = 0;
            do {
                while (col < 7) {
                    Object x = this.jTableCalendario.getValueAt(contadorAuxiliar, col);
                    if (x != null) {
                        int elementTable = Utilitaria.converteParaInteiro(x);
                        if (result.size() != 0) {
                            if (elementTable == result.get(0)) {
                                String date = Utilitaria.converteParaTexto(result.get(0));
                                this.jTextAreaDatas.append("-" + date + "-\n");
                                result.remove(0);
                            }
                        }
                    }
                    col++;
                }
                col = 0;
                contadorAuxiliar++;
            } while (contadorAuxiliar < totalDeLinhas);
        }
    }

    private void revalidaTableCalendar(int positionInicial, int valueTermina) {
        this.modeloCalendar.setNumRows(0);
        int cont = 0;

        switch (positionInicial) {
            case 0:
                this.modeloCalendar.addRow(new Object[]{
                    1, 2, 3, 4, 5, 6, 7
                });
                cont = cont + 7;
                break;
            case 1:
                this.modeloCalendar.addRow(new Object[]{
                    null, 1, 2, 3, 4, 5, 6
                });
                cont = cont + 6;
                break;
            case 2:
                this.modeloCalendar.addRow(new Object[]{
                    null, null, 1, 2, 3, 4, 5
                });
                cont = cont + 5;
                break;
            case 3:
                this.modeloCalendar.addRow(new Object[]{
                    null, null, null, 1, 2, 3, 4
                });
                cont = cont + 4;
                break;
            case 4:
                this.modeloCalendar.addRow(new Object[]{
                    null, null, null, null, 1, 2, 3
                });
                cont = cont + 3;
                break;
            case 5:
                this.modeloCalendar.addRow(new Object[]{
                    null, null, null, null, null, 1, 2
                });
                cont = cont + 2;
                break;
            case 6:
                this.modeloCalendar.addRow(new Object[]{
                    null, null, null, null, null, null, 1
                });
                cont = cont + 1;
                break;
        }
        cont++;

        while (cont <= valueTermina) {
            this.modeloCalendar.addRow(new Object[]{
                cont, cont + 1, cont + 2,
                cont + 3, cont + 4, cont + 5, cont + 6
            });
            cont = cont + 7;
        }
        for (int x = 0; x < 7; x++) {

            int linhas = this.modeloCalendar.getRowCount();

            Object n = this.modeloCalendar.getValueAt(linhas - 1, x);
            String text = String.valueOf(n);
            int inteiro = Integer.parseInt(text);

            if (inteiro > valueTermina) {
                this.modeloCalendar.setValueAt(null, linhas - 1, x);
            }
        }
    }

    private void ativacaoDeButtonsPrincipais(boolean ordem) {
        if (ordem) {
            this.btnNovoComp.setEnabled(true);
            this.btnEditarComp.setEnabled(true);
            this.btnExcluirComp.setEnabled(true);
        } else {
            this.btnNovoComp.setEnabled(false);
            this.btnEditarComp.setEnabled(false);
            this.btnExcluirComp.setEnabled(false);
        }
    }

    private void redimensionarColumnsComp() {
        this.jTableCompromissos.setAutoResizeMode(this.jTableCompromissos.AUTO_RESIZE_OFF);
        this.columnModel = this.jTableCompromissos.getColumnModel().getColumn(0);
        this.columnModel.setPreferredWidth(70);
        this.columnModelII = this.jTableCompromissos.getColumnModel().getColumn(1);
        this.columnModelII.setPreferredWidth(450);
    }

    public void initCompromissoDeHoje(String mes, String ano, int dia) {
        this.mesSelecionado = mes;
        this.anoSelecionado = ano;
        this.diaSelecionado = dia;

        this.calendar = new Calendario(this.mesSelecionado, this.anoSelecionado);
        ArrayList<Integer> valores = this.calendar.recuperarEstruturaCalendar();
        if (valores != null || !valores.isEmpty()) {
            revalidaTableCalendar(valores.get(0), valores.get(1));
            validaCompromissosCalendar(this.calendar.montaPerfilDeCompromissos());

            String date = this.diaSelecionado + "/" + this.mesSelecionado + "/" + this.anoSelecionado;
            this.lblDate.setText(date);
            preencheTabelaAgenda(this.diaSelecionado, this.mesSelecionado, this.anoSelecionado);
            jScrollTabelaAgenda.revalidate();
            this.jPanelHoraCompr.setVisible(true);
            this.jPanelHoraCompr.revalidate();

            this.jComboMesesCalendar.setSelectedIndex(
                    retornaIndexDeCombo(this.mesSelecionado, this.jComboMesesCalendar));

            this.jComboAnoCalendar.setSelectedIndex(
                    retornaIndexDeCombo(this.anoSelecionado, this.jComboAnoCalendar));
        }
    }

    private int retornaIndexDeCombo(String validation, JComboBox combo) {
        int contador = 0;
        while (combo.getItemAt(contador) != null) {
            if (validation.equals(combo.getItemAt(contador).toString())) {
                return contador;
            } else {
                contador++;
            }
        }
        return 0;
    }

    private void go() {
        if (!validaCombosCalendar()) {
            ApoioDeInterface.mensagemMesNaoIdentificado();
            jScrollPaneCalendar.setEnabled(false);
            return;
        } else {
            Object value = this.jComboMesesCalendar.getSelectedItem();
            String mes = Utilitaria.converteParaTexto(value);
            Object valueII = this.jComboAnoCalendar.getSelectedItem();
            String ano = Utilitaria.converteParaTexto(valueII);
            this.mesSelecionado = mes;
            this.anoSelecionado = ano;
            this.calendar = new Calendario(this.mesSelecionado, this.anoSelecionado);
            ArrayList<Integer> valores = this.calendar.recuperarEstruturaCalendar();
            if (valores != null || !valores.isEmpty()) {
                revalidaTableCalendar(valores.get(0), valores.get(1));
                validaCompromissosCalendar(this.calendar.montaPerfilDeCompromissos());
            }
        }
    }

    private void atualizaComprEvent() {
        preencheTabelaAgenda(this.diaSelecionado, this.mesSelecionado, this.anoSelecionado);
        this.jScrollTabelaAgenda.revalidate();
    }

    private void editarComprEvent() {
        try {
            if (this.diaSelecionado != 0 || this.mesSelecionado != null || this.anoSelecionado != null) {
                if (ApoioDeInterface.mensagemRealizarTarefa()) {
                    int numberLine = this.jTableCompromissos.getSelectedRow();
                    Object x = this.modeloTableAgenda.getValueAt(numberLine, 0);
                    String valorHora = String.valueOf(x);

                    this.edicaoComp = new EdicaoDeCompromisso();
                    this.edicaoComp.getLblRecebeHora().setText(valorHora);
                    this.edicaoComp.getLblRecebeDia().setText(String.valueOf(this.diaSelecionado));
                    this.edicaoComp.getLblRecebeMes().setText(this.mesSelecionado);
                    this.edicaoComp.getLblRecebeAno().setText(this.anoSelecionado);
                    this.edicaoComp.setVisible(true);
                }
            }
        } catch (ArrayIndexOutOfBoundsException erro) {
            ApoioDeInterface.mensagemDeNaoSelecao();
        }
    }

    private void deletarComprEvent() {
        try {
            if (this.diaSelecionado != 0 || this.mesSelecionado != null || this.anoSelecionado != null) {
                if (ApoioDeInterface.mensagemRealizarTarefa()) {
                    int numberLine = this.jTableCompromissos.getSelectedRow();
                    Object x = this.modeloTableAgenda.getValueAt(numberLine, 0);
                    String valor = String.valueOf(x);

                    this.conexao = new DBConection();
                    this.dbCompromisso = new DBCompromisso(this.conexao.getCon());
                    if (this.dbCompromisso.delete(valor) == 1) {
                        ApoioDeInterface.mensagemDeDelete();

                        //PREPARA TABLE
                        preencheTabelaAgenda(this.diaSelecionado, this.mesSelecionado, this.anoSelecionado);
                        jScrollTabelaAgenda.setVisible(true);
                        revalidate();
                    }

                }
            }
        } catch (SQLException erroSQL) {
            ApoioDeInterface.mensagemErroBanco(erroSQL.getMessage());
        } catch (ArrayIndexOutOfBoundsException erro) {
            ApoioDeInterface.mensagemDeNaoSelecao();
        }
    }

    @Override
    public void efeitoSonoro() {
        ExecutaSom exeSound = new ExecutaSom();
        exeSound.executar("/br/unipampa/sounds/click2.wav");
    }
}
